package com.resou.reader.bookstore.rank.fragment;

import android.widget.Toast;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.CommonData;
import com.resou.reader.api.entry.RankTypeBean;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.entry.TypeResultBean;
import com.resou.reader.api.service.StoreService;
import com.resou.reader.base.ResouBaseFragment;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.bookstore.rank.IRankContract;
import com.resou.reader.mine.IView.UserInstance;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Author: 萤火虫
 * E-mail:  Allensuns@163.com
 * Description:
 * CreateTime: 2019-04 -09
 **/
public class RankFragmentPresenter extends ResouBasePresenter<IRankFragmentContract.IRankFragmentView> implements IRankFragmentContract.IRankFragmentPresenter {
    StoreService storeService;


    public RankFragmentPresenter(IRankFragmentContract.IRankFragmentView view) {
        super(view);
        storeService = ApiImp.getInstance().getService(StoreService.class);
    }

    @Override
    public synchronized void quearTypeLabel(String rankChannel) {
        storeService.rankType(rankChannel).subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Observer<Result<List<RankTypeBean>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result<List<RankTypeBean>> listResult) {
                        mView.showTypeLabel(listResult.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showContent();
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {
                        mView.showContent();
                    }
                });
    }

    @Override
    public synchronized void quearContent(String typeId, int page) {
        if (page == 0) {
            mView.showProgress();
        }
        storeService.typelist(typeId, page, UserInstance.getToken())
                .subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Observer<Result<TypeResultBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result<TypeResultBean> commonDataResult) {
                        List<TypeResultBean.BookListBean> list = commonDataResult.getData().getBookList();
                        if (list != null && list.size() > 0) {
                            if (page == 0) {
                                mView.getSmartRefreshLayout().finishRefresh();
//                                mView.getSmartRefreshLayout().setNoMoreData(false);
                                mView.getAdapter().loadMoreComplete();
                            }
                            if (list.size() < 10) {
//                                mView.getSmartRefreshLayout().finishLoadMore(0, true, false);
                                mView.getAdapter().loadMoreEnd();
                            } else {
//                                mView.getSmartRefreshLayout().finishLoadMore();
                                mView.getAdapter().loadMoreComplete();
                            }
                            mView.showContent(list);
                        } else {
                            if (page == 0) {
                                mView.getSmartRefreshLayout().finishRefresh();
                                mView.getAdapter().loadMoreComplete();
                                mView.showEmpty();
                            } else {
//                                mView.getSmartRefreshLayout().finishLoadMoreWithNoMoreData();
                                mView.getAdapter().loadMoreEnd();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.getSmartRefreshLayout().finishRefresh();
                        mView.getSmartRefreshLayout().finishLoadMoreWithNoMoreData();
                        mView.getAdapter().loadMoreFail();
                        mView.showContent();
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {
                        mView.showContent();
                    }
                });
    }
}
